//
//  CALayer+Addition.m
//  dingding
//
//  Created by dingding on 15/12/10.
//  Copyright © 2015年 dingding. All rights reserved.
//
#import "UIView+Activity.h"
#import <objc/runtime.h>

typedef void (^clickBlock)();

@implementation UIView (Activity)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    //    self.width = size.width;
    //    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

-(void)showLoadAni
{
    UIView* view = [self viewWithTag:activityViewTag+2];
    if(view)return;
    
    float sizeW=140;
    float sizeH=250.;
    CGRect rect=[UIScreen mainScreen].bounds;
    CGPoint point=CGPointMake(rect.size.width/2-sizeW/2, rect.size.height/2-sizeH/2);
    point=[self convertPoint:point fromView:nil];
    view=[[UIView alloc] initWithFrame:
          CGRectMake(point.x, point.y, sizeW, sizeH)];
    
    UIImageView* img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apk_loadingone"]];
    NSArray* array=[NSArray arrayWithObjects:[UIImage imageNamed:@"apk_loadingone"], [UIImage imageNamed:@"apk_loadingtwo"],nil];
    
    img.frame=CGRectMake(sizeW/2-70/2, sizeH/2-105/2, 70, 105);
    [img setAnimationDuration:0.2f];
    [img setAnimationImages:array];
    [img startAnimating];
    [view addSubview:img];
    
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,sizeH-65,sizeW,30)];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=@"拼命加载中...";
    label.font=[UIFont systemFontOfSize:14.0f];
    label.textColor=[UIColor grayColor];
    [view addSubview:label];
    
    view.tag=activityViewTag+2;
    [self addSubview:view];
}

-(void)finishLoadAni
{
    UIView* view = [self viewWithTag:activityViewTag+2];
    if(view){
        [view removeFromSuperview];
    }
}

-(void)loadNone:(BOOL)remove func:(void(^)())func
{
    UIView* view = [self viewWithTag:activityViewTag+2];
    if(view){
        [view removeFromSuperview];
    }
    view = [self viewWithTag:activityViewTag+3];
    if(view){
        [view removeFromSuperview];
    }
    if(remove)return;
    objc_setAssociatedObject(self, @"cstblock", func, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    float sizeW=140;
    float sizeH=250.;
    CGRect rect=[UIScreen mainScreen].bounds;
    CGPoint point=CGPointMake(rect.size.width/2-sizeW/2, rect.size.height/2-sizeH/2);
    point=[self convertPoint:point fromView:nil];
    view=[[UIView alloc] initWithFrame:
          CGRectMake(point.x, point.y, sizeW, sizeH)];
    
    UIImageView* img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apk_girlone"]];
    img.frame=CGRectMake(sizeW/2-70/2, sizeH/2-105/2, 70, 105);
    [view addSubview:img];
    
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,sizeH-65,sizeW,30)];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=@"空空如也,点我刷新～";
    label.font=[UIFont systemFontOfSize:14.0f];
    label.textColor=[UIColor grayColor];
    [view addSubview:label];
    view.tag=activityViewTag+3;
    [self addSubview:view];
    [view setClickListener1:self action:@selector(clickNone:)];
}

-(void)clickNone:(id)sender
{
    [self loadNone:YES func:nil];
    clickBlock block=objc_getAssociatedObject(self, @"cstblock");
    block();
}

- (UIActivityIndicatorView*)createActivityViewAtCenter:(UIActivityIndicatorViewStyle)style
{
    static int size = 30;
    CGRect rect=[UIScreen mainScreen].bounds;
    UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
    activityView.frame = CGRectMake(rect.size.width/2 - size/2, rect.size.height/2 - size*2, size, size);
    activityView.tag = activityViewTag;
    [self addSubview:activityView];
    return activityView;
}

-(void)showWaitingView:(NSString*)msg
{
    UIView* view = [self viewWithTag:activityViewTag+1];
    if(view)return;
    self.userInteractionEnabled=NO;
    static int sizeW = 160;
    static int sizeH = 120;
    CGRect rect=[UIScreen mainScreen].bounds;
    CGPoint point=CGPointMake(rect.size.width/2-sizeW/2, rect.size.height/2-sizeH/2);
    point=[self convertPoint:point fromView:nil];
    UIView* waiting=[[UIView alloc] initWithFrame:
                     CGRectMake(point.x, point.y, sizeW, sizeH)];
    waiting.backgroundColor=[UIColor colorWithWhite:0.2 alpha:0.75];
    waiting.layer.cornerRadius=5;
    
    UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    static int sizeActi = 40;
    activityView.frame = CGRectMake(waiting.frame.size.width/2 - sizeActi/2, waiting.frame.size.height/2-sizeActi/2-20, sizeActi, sizeActi);
    [activityView startAnimating];
    [waiting addSubview:activityView];
    
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(waiting.frame.size.width/2-100,waiting.frame.size.height/2-sizeActi/2+15,200,sizeActi)];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=!msg?@"请稍后...":msg;
    label.font=[UIFont systemFontOfSize:14.0f];
    label.textColor=[UIColor whiteColor];
    [waiting addSubview:label];
    waiting.tag=activityViewTag+1;
    [self addSubview:waiting];
}

-(void)dismissWaitingView
{
    self.userInteractionEnabled=YES;
    UIView* view = [self viewWithTag:activityViewTag+1];
    if(view){
        [view removeFromSuperview];
    }
}

- (UIActivityIndicatorView*)getActivityViewAtCenter
{
    UIView* view = [self viewWithTag:activityViewTag];
    if (view != nil && [view isKindOfClass:[UIActivityIndicatorView class]]){
        return (UIActivityIndicatorView*)view;
    }
    else {
        return nil;
    }
}

- (void)showActivityViewAtCenter
{
    UIActivityIndicatorView* activityView = [self getActivityViewAtCenter];
    if (activityView == nil){
        activityView = [self createActivityViewAtCenter:UIActivityIndicatorViewStyleGray];
    }
    [activityView startAnimating];
}

- (void)hideActivityViewAtCenter
{
    UIActivityIndicatorView* activityView = [self getActivityViewAtCenter];
    if (activityView != nil){
        [activityView stopAnimating];
    }
}

- (void)showActivityViewAtViewCenter
{
    UIActivityIndicatorView* activityView = [self getActivityViewAtCenter];
    if (activityView == nil){
        activityView = [self createActivityViewAtCenter:UIActivityIndicatorViewStyleGray];
    }
    activityView.frame=CGRectMake(self.bounds.size.width/2-activityView.bounds.size.width/2,
                                  self.bounds.size.height/2-activityView.bounds.size.height/2,
                                  activityView.bounds.size.width, activityView.bounds.size.height);
    [activityView startAnimating];
}

- (NSString*)click {
    return objc_getAssociatedObject(self, @selector(click));
}
-(void)setClick:(NSString*)click
{
    objc_setAssociatedObject(self, @selector(click), click, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setClickListener:(id)target action:(SEL)action
{
    self.click=@"click";
    
    static UITapGestureRecognizer * mytap;
    if(mytap){
        [self removeGestureRecognizer:mytap];
    }
    mytap=[[UITapGestureRecognizer alloc]initWithTarget:target action:action];
    [self addGestureRecognizer:mytap];
}

-(void)setClickListener1:(id)target action:(SEL)action
{
    self.click=@"click1";
    static UITapGestureRecognizer * mytap;
    if(mytap){
        [self removeGestureRecognizer:mytap];
    }
    mytap=[[UITapGestureRecognizer alloc]initWithTarget:target action:action];
    [self addGestureRecognizer:mytap];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
     if([self.click isEqualToString:@"click"])
     {
         [self setBackgroundColor:[UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1.0]];
     }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if([self.click isEqualToString:@"click"])
    {
        [self setBackgroundColor:[UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1.0]];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if([self.click isEqualToString:@"click"])
    {
         [self setBackgroundColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
    }
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    if([self.click isEqualToString:@"click"])
    {
         [self setBackgroundColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
    }
}

-(BOOL)isUpdateDatas{
    NSNumber *num=objc_getAssociatedObject(self, @selector(isUpdateDatas));
    if(num){
        return [num intValue]==1;
    }
    return NO;
}
-(void)setIsUpdateDatas:(BOOL)isUpdateDatas
{
    int a;
    if(isUpdateDatas){
        a=1;
    }else{
        a=0;
    }
    objc_setAssociatedObject(self, @selector(isUpdateDatas), [NSNumber numberWithInt:a], OBJC_ASSOCIATION_ASSIGN);
}

-(void)updateDatas{
    self.isUpdateDatas=NO;
    NSLog(@"reset update");
}

@end
